

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.handlewell.generator.service.ISourceServ;
import com.handlewell.generator.service.impl.SourceServImpl;
import com.handlewell.generator.support.Constraints;
import com.handlewell.generator.support.DBUtils;
import com.handlewell.generator.support.TableColumns;
import com.handlewell.generator.support.VelocityUtil;

/** 
 * 功能概述： <br> 
 * 
 * @author 倪敏 
 * 
 */
public class AutoGenerator {

	public static HashMap<String,String> sdb=new HashMap<String,String>();
	static {
		sdb.put(Constraints.driverClass, 	"com.mysql.jdbc.Driver");
		sdb.put(Constraints.url, 			"jdbc:mysql://localhost:3306/LYHEAT");
		sdb.put(Constraints.user, 			"root");
		sdb.put(Constraints.password, 		"root");
	}
	
	public static List<HashMap<String,String>> srlist=new ArrayList<HashMap<String,String>>();
	static {
		HashMap<String,String> r1=new HashMap<String,String>();
		//路径设置
		r1.put(Constraints.dir_template,"template/");
		r1.put(Constraints.dir_src,		"build/src/com/bjtech/");
		r1.put(Constraints.dir_web,		"build/WebRoot/");
		r1.put(Constraints.dir_spring,	"build/WebRoot/WEB-INF/spring/");
		r1.put(Constraints.dir_struts,	"build/src/struts/");
		r1.put(Constraints.v_package, 	"com.bjtech");
		
		
		r1.put(Constraints.v_author, 	"王峰");
		r1.put(Constraints.v_desc, 		"登陆用户信息");
		r1.put(Constraints.v_system, 	"management");
		r1.put(Constraints.v_service, 	"loginUserInfo");
		r1.put(Constraints.v_service2, 	upper(r1.get(Constraints.v_service)));
		r1.put(Constraints.v_object, 	"loginUserInfo");
		r1.put(Constraints.v_object2, 	upper(r1.get(Constraints.v_object)));
		r1.put(Constraints.v_table, 	"LOGIN_USER_INFO");
		srlist.add(r1);
		
	}
	
	/** 
	 * @param args 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AutoGenerator ag=new AutoGenerator();
		ag.generateResource(sdb,srlist);
	}
	
	private void generateResource(HashMap<String,String> config,List<HashMap<String,String>> rlist) {
		try {
			VelocityUtil velocityUtil = new VelocityUtil();
			velocityUtil.init();
						
			// 获取数据库资源
			DBUtils dbutil = new DBUtils(config);
			
			//获得资源生成器实例
			ISourceServ sourceServ=new SourceServImpl();
			
			//循环生成所需的资源
			for (HashMap<String,String> item:rlist){
				//获得表列信息
				List<TableColumns> list = dbutil.getTable(item.get(Constraints.v_table), true);
				
				//设置生成器初始资源
				sourceServ.initService(item, list, velocityUtil);

				//配置domain目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"domain.vm",item.get(Constraints.dir_src)+"/domain",upper(item.get(Constraints.v_object))+".java");
				//配置querySpec目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"querySpec.vm",item.get(Constraints.dir_src)+"/domain/query",upper(item.get(Constraints.v_object))+"QuerySpec.java");
				//配置dao目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"dao.vm",item.get(Constraints.dir_src)+"/dao",upper(item.get(Constraints.v_object))+"Dao.java");
				//配置daoImpl目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"daoImpl.vm",item.get(Constraints.dir_src)+"/dao/ibatis","SqlMap"+upper(item.get(Constraints.v_object))+"Dao.java");
				//配置sqlMap目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"SqlMap.vm",item.get(Constraints.dir_src)+"/dao/ibatis/maps",item.get(Constraints.v_table)+"_SqlMap.xml");
				//配置action目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"action.vm",item.get(Constraints.dir_src)+"/web/action/"+item.get(Constraints.v_system),item.get(Constraints.v_object2)+"Action.java");
				//配置list目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"list.vm",item.get(Constraints.dir_web)+item.get(Constraints.v_system)+"/"+upper(item.get(Constraints.v_object)),"/list.jsp");
				//配置form目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"form.vm",item.get(Constraints.dir_web)+item.get(Constraints.v_system)+"/"+upper(item.get(Constraints.v_object)),"/form.jsp");
				//配置view目录
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"view.vm",item.get(Constraints.dir_web)+item.get(Constraints.v_system)+"/"+upper(item.get(Constraints.v_object)),"/view.jsp");
				//SqlMapDao的Spring配置文件
			//	sourceServ.generate(item.get(Constraints.dir_template)+"/"+"dataAccessContext.vm",item.get(Constraints.dir_web)+"/WEB-INF/config/spring","/"+upper(item.get(Constraints.v_object))+"_dataAccessContext.xml");
				//ibatis配置文件
				sourceServ.generate(item.get(Constraints.dir_template)+"/"+"sqlMapConfig.vm",item.get(Constraints.dir_web)+"/WEB-INF/config/ibatis","/"+upper(item.get(Constraints.v_object))+"_sqlMapConfig.xml");
				System.out.println("["+item.get(Constraints.v_desc)+"]功能资源生成完毕!");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String upper(String name){
		return name.substring(0,1).toUpperCase()+name.substring(1);
	}
}
